#  Copyright (c) 2021- ~, XIWANG
#  
#  Python module defined to solve datacube analysis problems
#  Used in Python3
#  Note in https://www.yuque.com/wangxi_chn
#
#  --------------------
#  |                  | 
#  |                  | cubelines = cube.shape[1]
#  |                  |
#  --------------------
#      cubecolumns = cube.shape[2]        cubelayers = cube.shape[0]         
# 
#  
#  Change Logs:
#  Date           Author       Notes			Mail
#  2021-2-28     XiWang   	   first version	WangXi_Chn@foxmail.com

import numpy as np

def sheet2cube(datasheet,cubecolumns,cubelines,cubelayers):
    datacube = np.zeros((cubelayers,cubelines,cubecolumns))
    for index in range(1,cubelayers+1):
        datacube[index-1,:,:] = datasheet[cubelines*(index-1):(index*cubelines),:]
    return datacube

def cubecolAvezip(datacube,cubecolumnsIndex):
    dataline = np.zeros(datacube.shape[0])
    for i in range(1,datacube.shape[0]+1):
        _sum = 0
        _count = 0
        for j in range(1,datacube.shape[1]+1):
            if np.isnan(datacube[i-1,j-1,cubecolumnsIndex]):
                datacube[i-1,j-1,cubecolumnsIndex] = 0
            _sum += datacube[i-1,j-1,cubecolumnsIndex]
            _count = _count + 1
        dataline[i-1]=_sum/_count
    return dataline
